<?php

namespace app\common\model;

use think\Model;


class Cdkey extends Model
{

    

    

    // 表名
    protected $name = 'cdkey';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_start_text',
        'overduetime_text',
        'is_use_text'
    ];
    

    
    public function getIsStartList()
    {
        return ['0' => __('Is_start 0'), '1' => __('Is_start 1')];
    }

    public function getIsUseList()
    {
        return ['0' => __('Is_use 0'), '1' => __('Is_use 1')];
    }


    public function getIsStartTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_start']) ? $data['is_start'] : '');
        $list = $this->getIsStartList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOverduetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['overduetime']) ? $data['overduetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsUseTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_use']) ? $data['is_use'] : '');
        $list = $this->getIsUseList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setOverduetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function depot()
    {
        return $this->belongsTo('Depot', 'depot_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
